<?php
class loginModel extends baseModel{
    protected $appID = 'user';

    public function __construct(){
        parent::__construct();
        $this->appID = empty($appID) ? $this->appID : $appID;
        $config=require(BASE_PATH . '/config.php');
        $this->appID = $config['APP']['COOKIE_PREFIX'].$this->appID;
    }

    //登录相关
    public function check_login()
    {
        if(!$this->is_login()){
            if(is_ajax()){
                $this->msg('您尚未登录，请登录后操作！',0);
            }else{
                $this->redirect( url('member/login/index') );
            }
            
        }
    }

    //功能权限
    public function check_purview()
    {
        $info=$this->user_info();
        $user_group=model('user_group')->info($info['gid']);
        $base_purview=unserialize($user_group['base_purview']);
        $purview_info=api(APP_NAME,'hook_purview_user');
        if(empty($purview_info)){
            return true;
        }
        $controller=$purview_info[CONTROLLER_NAME];
        if(empty($controller['auth'])){
            return true;
        }
        $action=$controller['auth'][ACTION_NAME];
        if(empty($action)){
            return true;
        }

        $current=APP_NAME.'_'.CONTROLLER_NAME.'_'.ACTION_NAME;

        if(!in_array($current, (array)$base_purview)){
            $this->msg('您没有权限访问此功能！',0);
        }
        return true;
    }

    //判断是否登录
    public function is_login()
    {
        $userid=intval(get_cookie($this->appID . '_uid'));
        if(empty($userid)){
            return false;
        }else{
            return true;
        }
    }

    //获取当前用户信息
    public function user_info()
    {
        $uid=intval(get_cookie($this->appID . '_uid'));
        if(empty($uid)){
            return false;
        }
        return model('user_list')->info($uid);
    }

	
}